﻿package utils
{
	import flash.events.*;
	import flash.xml.*;
	import flash.net.*;
	import data.Feed
	
	public class XMLimport extends EventDispatcher 
	{
		private var data:Array;
		private var loader:URLLoader;

		public var xml:XML=new XML;
		
		public function XMLimport()
		{
			loader = new URLLoader;
			loader.addEventListener(Event.COMPLETE,onLoadedXML);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,statusHandler);
		}
		
		public function load(file:String):void 
		{
			try 
			{
			loader.load(new URLRequest(file));
			} 
			catch (e:Error) 
			{
				trace('failed to load file: ' + file);
			}
		}
		
		public function onLoadedXML(e:Event):void 
		{
			try 
			{
				var dat:XML=XML(e.target.data);
			} 
			catch (e:Error) 
			{
				trace('xml failed to load: ' + e.message);
				
				return;
			}
			
			data = XMLparser.parse(dat);
			
			dispatchEvent(new Event(Event.COMPLETE));
		}

		private function statusHandler(evt:HTTPStatusEvent):void
		{
			//status=evt.status;
		}
		
		/** Catch security and io errors **/
		private function errorHandler(evt:ErrorEvent):void
		{
			//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,status + ': ' + evt.text));
		}
		
	}
}